#include<cstdio>
#include<cstring>
const int maxn=1300000;
int p[110000],f[maxn];
bool prim[maxn];
int count=0;
int main()
{
	memset(prim,1,sizeof(prim));
	memset(f,0,sizeof(f));
	prim[0]=prim[1]=0;
	for(int i=2;i<maxn;i++)
		if(prim[i])
		{
			p[++count]=i;
			f[i]=0;
			for(int j=i;j<maxn;j+=i)
				prim[j]=0;
		}
	for(int i=2;i<count;i++)
		for(int j=p[i]+1;j<p[i+1];j++)
			f[j]=p[i+1]-p[i];
	int n;
	while(scanf("%d",&n)&&n)
		printf("%d\n",f[n]);
	return 0;
}

